#!/bin/sh
#!/bin/dash
# check ssh from
# 0----from Internet
# 1----from local, no ssh
# 2----from Lab
#对变量赋值：
# based on zhc's work of graduation project
. /usr/share/linuxer/etc/config
stdin=$1
password=$2
Result=$3
sshFromOuter()
{
# Get IP where user 'ssh'es from
CLIENT_IP=$(echo $SSH_CLIENT | awk '{print $1}')

if [ -z $CLIENT_IP ] 
  then
    return 1
  else
    if [[ $CLIENT_IP =~ ${LAB_IP_PATTERN} ]]
      then
        return 2
      else
        return 0
    fi
fi
}

create()
{
if ! docker inspect $USER > /dev/null 2>&1 
  then
    if [ `docker ps -a |wc -l` -gt $MAX_CONTAINER ]
    then
      docker rm -f `docker ps -aq |tail -n1`
    fi
    PASSWORD=$password
    docker run --security-opt seccomp=unconfined \
    --name $USER \
    -P --expose 8888 \
    --user root \
    -e PASSWORD="$PASSWORD"  \
    -e NB_UID=$(id -u $USER) \
    -e GRANT_SUDO=yes \
    -v "${PWD%/*}":/notebooks \
    -d $IMAGE \
    start-notebook.sh \
    --notebook-dir=/notebooks \
    --ip=0.0.0.0 \
    > /dev/null 2>&1
  fi
}

start()
{
docker inspect $USER > /dev/null 2>&1
if [ $? -ne 0 ]
  then
    create
fi

myaddress

}

stop()
{
docker inspect $USER > /dev/null 2>&1
if [ $? -eq 0 ]
  then
    docker rm -f $USER > /dev/null 2>&1
fi

docker inspect $USER > /dev/null 2>&1
if [ ! $? -eq 0 ]
  then
    echo "STOPED|"
fi
}

submit()
{

sshFromOuter

#if [ $? -eq 1 ]
#  then
#    dialog --title "Alert" --backtitle "Main-->Submit" --clear --msgbox "Submit locally is not supported now!" 15 40
#    return 1
#fi

PWD=`pwd`
EXP=${PWD%/*}
EXP=${EXP##*/}
DIRFULL=${SUBMIT_DIR}/${USER}/${EXP}

umask 0072

if [[ ! ${EXP} =~ ${EXP_PATTERN} ]]
  then
    echo "RUNAGAIN|"
  else
    if [ -d ${DIRFULL} ]
      then
	   echo "EXITS|"
        if [[ $Result != 0 ]]
          then
            return
          else
            rm -rf ${DIRFULL} >/dev/null 2>&1
        fi
    fi

  mkdir -p ${DIRFULL} >/dev/null 2>&1 &&\
  cp -rf ./ ${DIRFULL} >/dev/null 2>&1
  if [ $? -eq 0 ]  
    then
      echo "SUCCESS|"
      echo "${DIRFULL}|"
      SUBMIT=$(tree -f -pD ${DIRFULL})
      echo "${SUBMIT}|"
    else
      echo "SUBMITFAIL|"
  fi
fi
}

state()
{
DOCKER_STATUS=$(docker inspect $USER)
echo "${DOCKER_STATUS}|"
}

check()
{
echo "$USER|"
}

myaddress()
{
#CONTAINER_IP=$(docker inspect $USER | grep "IPAddress" | grep -Pom 1 '[0-9.]{7,15}')
CONTAINER_IP=$(docker inspect $USER -f {{.NetworkSettings.IPAddress}})
PORT=$(docker inspect $USER -f '{{(index (index .NetworkSettings.Ports "8888/tcp") 0).HostPort}}')

if [ -z ${CONTAINER_IP} ]
  then
    echo "UNSTARTED|"
    return 1
fi

if sshFromOuter
  then
    IP=${PUBLIC_DOMAIN}
  else
    # start from lab or locally
#    IP=${CONTAINER_IP}
#    PORT=8888
#    IP=$(ip route get 1 | awk '{print $NF;exit}')
    IP=$HOST_IP
    if [ -z ${HOST_IP} ]
    then
      IP=$(ip route get 1 | awk '{print $NF;exit}')
    fi
fi

echo "http://${IP}:${PORT}/|"

}


score()
{

PWD=`pwd`
EXP=${PWD##*/}

LABDIRFULL=${SUBMIT_DIR}/${USER}/${EXP}
DIRFULL=$SCORE_DIR/$EXP
echo $DIRFULL
echo $LABDIRFULL
if [ ! -f $DIRFULL ]
  then
    echo "NOTCKECKING|"
    return 1
fi
if [ -f $DIRFULL ]
  then
    score=`cat $DIRFULL | grep "^${USER}" | while IFS="," read v1 v2 v3 v4
    do
      echo "Score: $v2|"
      echo "Marker: $v3|" 
      echo "Date: $v4|"
    done`
    if [ ! -z "$score" ]
      then
	cp -rf "${LABDIRFULL}" "${PWD}/"
        echo "$score\n Visit directory ${EXP}/ to see the comments|"
      else
        echo "NOTCHECKED|"
    fi
fi
}

#判断
case $stdin in
  1)
    start
    ;;
  "2")
    stop
    ;;
  "3")
    submit
    ;;
  "4")
    myaddress
    ;;
  "5")
    score
    ;;
  "6")
    state
    ;;
  "7")
    check
    ;;
esac
